package com.atguigu.zhxy.controller;

import com.atguigu.zhxy.pojo.Admin;
import com.atguigu.zhxy.pojo.Student;
import com.atguigu.zhxy.service.StudentService;
import com.atguigu.zhxy.service.TbAdminService;
import com.atguigu.zhxy.util.JwtHelper;
import com.atguigu.zhxy.util.MD5;
import com.atguigu.zhxy.util.Result;
import com.atguigu.zhxy.util.ResultCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sms/adminController")
public class AdminController {

    @Autowired
    private TbAdminService tbAdminService;

    @ApiOperation("添加或修改Admin信息")
    @PostMapping("/saveOrUpdateAdmin")
    public Result saveOrUpdateAdmin(@RequestBody Admin admin){
        if (admin.getPassword()!=null) {
            //密码加密
            admin.setPassword(MD5.encrypt(admin.getPassword()));
        }
        tbAdminService.saveOrUpdate(admin);
        return Result.ok();
    }



    @ApiOperation("删除Admin信息")
    @DeleteMapping("/deleteAdmin")
    public Result deleteAdmin(@RequestBody List<Integer> ids){
        tbAdminService.removeByIds(ids);
        return Result.ok();
    }

    @ApiOperation("分页获取所有Admin信息【带条件】")
    @GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
    public Result getAllAdmin(
            @PathVariable Integer pageNo,
            @PathVariable Integer pageSize,
            String adminName
    ){
        Page<Admin> pageParam = new Page<>(pageNo, pageSize);
        IPage<Admin> iPage = tbAdminService.getAdmins(pageParam,adminName);
        return Result.ok(iPage);
    }

}
